// (c) 2007 Messaging Logic. All Rights Reserved.
package org.xi8ix.jms;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import java.util.Enumeration;
import java.util.HashMap;

/**
 * @author Iain Shigeoka
 */
public class JMSMessage implements Message {

    private String JMSMessageID;
    private long JMSTimestamp;
    private String JMSCorrelationID;
    private Destination JMSReplyTo;
    private Destination destination;
    private int JMSDeliveryMode;
    private boolean JMSRedelivered;
    private String JMSType;
    private long JMSExpiration;
    private int JMSPriority;
    private HashMap<String,Object> properties = new HashMap<String,Object>();

    public JMSMessage() {

    }

    public JMSMessage(JMSMessage message) {
        this.JMSMessageID = message.JMSMessageID;
        this.JMSTimestamp = message.JMSTimestamp;
        this.JMSCorrelationID = message.JMSCorrelationID;
        this.JMSReplyTo = message.JMSReplyTo;
        this.destination = message.destination;
        this.JMSDeliveryMode = message.JMSDeliveryMode;
        this.JMSRedelivered = message.JMSRedelivered;
        this.JMSType = message.JMSType;
        this.JMSExpiration = message.JMSExpiration;
        this.JMSPriority = message.JMSPriority;
        properties = new HashMap<String,Object>(message.properties);
    }

    public String getJMSMessageID() throws JMSException {
        return JMSMessageID;
    }

    public void setJMSMessageID(String messageID) throws JMSException {
        this.JMSMessageID = messageID;
    }

    public long getJMSTimestamp() throws JMSException {
        return JMSTimestamp;
    }

    public void setJMSTimestamp(long timestamp) throws JMSException {
        this.JMSTimestamp = timestamp;
    }

    public byte[] getJMSCorrelationIDAsBytes() throws JMSException {
        if (JMSCorrelationID == null) {
            return new byte[0];
        } else {
            return JMSCorrelationID.getBytes();
        }
    }

    public void setJMSCorrelationIDAsBytes(byte[] bytes) throws JMSException {
        JMSCorrelationID = new String(bytes);
    }

    public void setJMSCorrelationID(String string) throws JMSException {
        JMSCorrelationID = string;
    }

    public String getJMSCorrelationID() throws JMSException {
        return JMSCorrelationID;
    }

    public Destination getJMSReplyTo() throws JMSException {
        return JMSReplyTo;
    }

    public void setJMSReplyTo(Destination destination) throws JMSException {
        JMSReplyTo = destination;
    }

    public Destination getJMSDestination() throws JMSException {
        return destination;
    }

    public void setJMSDestination(Destination destination) throws JMSException {
        this.destination = destination;
    }

    public int getJMSDeliveryMode() throws JMSException {
        return JMSDeliveryMode;
    }

    public void setJMSDeliveryMode(int i) throws JMSException {
        this.JMSDeliveryMode = i;
    }

    public boolean getJMSRedelivered() throws JMSException {
        return JMSRedelivered;
    }

    public void setJMSRedelivered(boolean b) throws JMSException {
        this.JMSRedelivered = b;
    }

    public String getJMSType() throws JMSException {
        return JMSType;
    }

    public void setJMSType(String string) throws JMSException {
        this.JMSType = string;
    }

    public long getJMSExpiration() throws JMSException {
        return JMSExpiration;
    }

    public void setJMSExpiration(long l) throws JMSException {
        this.JMSExpiration = l;
    }

    public int getJMSPriority() throws JMSException {
        return JMSPriority;
    }

    public void setJMSPriority(int i) throws JMSException {
        this.JMSPriority = i;
    }

    public void clearProperties() throws JMSException {
        properties.clear();
    }

    public boolean propertyExists(String name) throws JMSException {
        return properties.containsKey(name);
    }

    public boolean getBooleanProperty(String name) throws JMSException {
        return properties.containsKey(name) && (Boolean) properties.get(name);
    }

    public byte getByteProperty(String name) throws JMSException {
        if (properties.containsKey(name)) {
            return (Byte)properties.get(name);
        } else {
            return 0;
        }
    }

    public short getShortProperty(String name) throws JMSException {
        if (properties.containsKey(name)) {
            return (Short)properties.get(name);
        } else {
            return 0;
        }
    }

    public int getIntProperty(String name) throws JMSException {
        if (properties.containsKey(name)) {
            return (Integer)properties.get(name);
        } else {
            return 0;
        }
    }

    public long getLongProperty(String name) throws JMSException {
        if (properties.containsKey(name)) {
            return (Long)properties.get(name);
        } else {
            return 0;
        }
    }

    public float getFloatProperty(String name) throws JMSException {
        if (properties.containsKey(name)) {
            return (Float)properties.get(name);
        } else {
            return 0;
        }
    }

    public double getDoubleProperty(String name) throws JMSException {
        if (properties.containsKey(name)) {
            return (Double)properties.get(name);
        } else {
            return 0;
        }
    }

    public String getStringProperty(String name) throws JMSException {
        return (String)properties.get(name);
    }

    public Object getObjectProperty(String name) throws JMSException {
        return properties.get(name);
    }

    public Enumeration getPropertyNames() throws JMSException {
        return new IteratorEnumerator<String>(properties.keySet().iterator());
    }

    public void setBooleanProperty(String name, boolean value) throws JMSException {
        properties.put(name,value);
    }

    public void setByteProperty(String name, byte value) throws JMSException {
        properties.put(name,value);
    }

    public void setShortProperty(String name, short value) throws JMSException {
        properties.put(name,value);
    }

    public void setIntProperty(String name, int value) throws JMSException {
        properties.put(name,value);
    }

    public void setLongProperty(String name, long value) throws JMSException {
        properties.put(name,value);
    }

    public void setFloatProperty(String name, float value) throws JMSException {
        properties.put(name,value);
    }

    public void setDoubleProperty(String name, double value) throws JMSException {
        properties.put(name,value);
    }

    public void setStringProperty(String name, String value) throws JMSException {
        properties.put(name,value);
    }

    public void setObjectProperty(String name, Object value) throws JMSException {
        properties.put(name,value);
    }

    public void acknowledge() throws JMSException {
    }

    public void clearBody() throws JMSException {
    }
}
